﻿#include "u_User_page.h"

UserPage::UserPage(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();
    this->UpdateTable();

    this->setTitle("用户管理");
    this->setMaxHide();
    this->resize(800, 500);
}

void UserPage::InitWidget(){
    btnEdit = new QPushButton(QChar(0xe657) + QString(" 保存"));
    btnDel  = new QPushButton(QChar(0xe603) + QString(" 删除"));

    editCode = new QLineEdit();
    editPassword = new QLineEdit(); editPassword->setEchoMode(QLineEdit::Password);
    editName = new QLineEdit();
    editRole = new QComboBox(); editRole->addItems({"管理员", "机房主任", "操作员"});

    QStringList headers = QString("账号,密码,姓名,角色").split(",");
    tableWidget = MyGUI::GetTableWidget(headers, 180);

    QGridLayout *layout1 = new QGridLayout();
    layout1->setMargin(10);
    layout1->setSpacing(10);
    layout1->addWidget(new QLabel("账号"),      1,1,1,1);
    layout1->addWidget(editCode,                1,2,1,1);
    layout1->addWidget(new QLabel("密码"),      1,3,1,1);
    layout1->addWidget(editPassword,            1,4,1,1);
    layout1->addWidget(new QLabel("姓名"),      1,5,1,1);
    layout1->addWidget(editName,                1,6,1,1);
    layout1->addWidget(new QLabel("角色"),      1,7,1,1);
    layout1->addWidget(editRole,                1,8,1,1);
    layout1->addWidget(btnEdit,                 1,9,1,1);
    layout1->addWidget(btnDel,                  1,10,1,1);

    QGroupBox *groupbox11 = new QGroupBox();
    groupbox11->setStyleSheet("margin-top: 0px;");
    groupbox11->setLayout(layout1);

    //-------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(10);
    if(globalData->CheckUserRole(0)){
        mainLayout->addWidget(groupbox11);
    }
    mainLayout->addWidget(tableWidget);
    this->addLayout(mainLayout);
}

void UserPage::SetupConnections(){
    connect(btnEdit , SIGNAL(clicked()), this, SLOT(OnBtnEditClicked()));
    connect(btnDel , SIGNAL(clicked()), this, SLOT(OnBtnDelClicked()));
    connect(tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(OnTableDoubleClicked(int,int)));
}

void UserPage::Initialize(){
    queryList = globalData->sqlBase->GetUserList();
}

void UserPage::UpdateTable(){
    int count = queryList.size();

    tableWidget->setRowCount(0);
    tableWidget->setRowCount(count);

    for(int i=0; i<count; i++){
        User info = queryList.at(i);

        tableWidget->setItem(i, 0, MyGUI::GetTableWidgetItem(info.code));
        tableWidget->setItem(i, 1, MyGUI::GetTableWidgetItem("******"));
        tableWidget->setItem(i, 2, MyGUI::GetTableWidgetItem(info.name));
        tableWidget->setItem(i, 3, MyGUI::GetTableWidgetItem(info.GetRole()));
    }
}

void UserPage::OnBtnEditClicked(){
    if(this->editCode->text().isEmpty()){ editCode->setFocus(); MyGUI::ShowMessageTip("请输入账号", this); return; }
    if(this->editPassword->text().isEmpty()){ editPassword->setFocus(); MyGUI::ShowMessageTip("请输入密码", this); return; }
    if(this->editName->text().isEmpty()){ editName->setFocus(); MyGUI::ShowMessageTip("请输入姓名", this); return; }

    if(!MyGUI::ShowMessageBoxQuesion("确定要保存吗？", this)){
        return;
    }

    User info;
    info.code = this->editCode->text().trimmed();
    info.password = this->editPassword->text().trimmed();
    info.name = this->editName->text().trimmed();
    info.role = this->editRole->currentIndex();
    info.builtin = false;

    bool b = globalData->sqlBase->SaveUser(info);

    this->Initialize();
    this->UpdateTable();

    if(b){
        MyGUI::ShowMessageBoxInfo("保存成功", this);
    }else{
        MyGUI::ShowMessageBoxError("保存失败", this);
    }

}

void UserPage::OnBtnDelClicked(){
    int row = tableWidget->currentRow();
    if(row < 0){
        MyGUI::ShowMessageBoxError("请选择要删除的记录", this);
        return;
    }

    User info = queryList.at(row);
    if(info.builtin){
        MyGUI::ShowMessageBoxError("内置账号不可删除", this);
        return;
    }

    if(!MyGUI::ShowMessageBoxQuesion("确定要删除选中的记录？", this)){
        return;
    }

    bool b = globalData->sqlBase->DelUser(info.code);

    this->Initialize();
    this->UpdateTable();

    if(b){
        MyGUI::ShowMessageBoxInfo("删除成功", this);
    }else{
        MyGUI::ShowMessageBoxError("删除失败", this);
    }
}

void UserPage::OnTableDoubleClicked(int row, int column){
    User info = queryList.at(row);

    if(info.builtin){
        User info1;
        info1.role = 0;
        info = info1;

        MyGUI::ShowMessageBoxError("内置账号不可编辑", this);
    }

    editCode->setText(info.code);
    editPassword->setText(info.password);
    editName->setText(info.name);
    editRole->setCurrentIndex(info.role);
}
